﻿using System;
using System.Collections.Generic;
using System.Collections;

/// <summary>
/// 游戏数据核心
/// </summary>
public class GameDataKernel
{
	static GameDataKernel _inst;

	public static GameDataKernel inst {
		get {
			if (_inst == null) {
				_inst = new GameDataKernel ();
			}
			return _inst;
		}
	}

	///////////////////////////////////////////////////////////////////////////

	public const int READY_PLAYERS_COUNT = 3 - 1;

	///////////////////////////////////////////////////////////////////////////
	// 数据类型

	public class GamePlayer
	{
		bool _ready;
		// 就绪
		bool _dealer;
		// 庄
		bool _master;
		// 房主
		string _region;
		// 地区
		string _headpic;
		// 头像
		string _name;
		// 名字
		int _gold;
		// 金币
		int _uid;
		// 玩家UID
		bool _online;
		// 是否在线
		int _sex;
		// 性别
		string _ip;
		// IP地址

		int _index;
		// 位置
		List<int> _cards;
		// 牌

		public GamePlayer ()
		{
			_ready = _dealer = _master = false;
			_region = _headpic = _name = "";
			_gold = _uid = 0;
			_online = false;
			_sex = 0;
			_ip = "";
			_index = 0;
			_cards = new List<int> ();
		}

		public void Init (HashtableEx initArgs, int index)
		{
			_uid = initArgs.GetInt ("uid");
			_ready = initArgs.GetBoolean ("ready");
			_online = initArgs.GetBoolean ("online");
			_name = initArgs.GetString ("name");
			_headpic = initArgs.GetString ("headpic");
			_sex = initArgs.GetInt ("sex");
			_ip = initArgs.GetString ("ip");
			_index = index;
			_gold = initArgs.GetInt ("score");

			if (initArgs.ContainsKey ("cards")) {
				cards.AddRange (initArgs.GetInts2 ("cards"));
			}
		}

		public void DecCards (int[] cards, bool check = false)
		{
			if (check) {
				foreach (int card in cards) {
					_cards.Remove (card);
				}
			} else {
				_cards.RemoveRange (0, cards.Length);
			}
		}

		public void AddCard (int card, bool check = false)
		{
			if (check) {
				_cards.Add (card);
			} else {
				_cards.Add (0);
			}
		}

		public void Reset ()
		{
			_cards.Clear ();

			_ready = false;
		}

		///////////////////////////////////////////////////////////////////////
		// Getter & Setter
		public bool ready { get { return _ready; } set { _ready = value; } }

		public bool dealer { get { return _dealer; } set { _dealer = value; } }

		public bool master { get { return _master; } set { _master = value; } }

		public bool online { get { return _online; } set { _online = value; } }

		public string reagion { get { return _region; } }

		public string headpic { get { return _headpic; } }

		public string name { get { return _name; } }

		public int gold { get { return _gold; } set { _gold = value; } }

		public int index { get { return _index; } }

		public List<int> cards { get { return _cards; } }

		public string ip { get { return _ip; } }

		public int sex { get { return _sex; } }
	}

	public class RoundSett
	{
		public class PlayerSett
		{
			string _name;
			string _headpic;
			int _score;
			int _roundScore;
			bool _win;

			int _index;

			public PlayerSett (int index)
			{
				_name = _headpic = "";
				_score = _roundScore = 0;
				_win = false;
				_index = index;
			}

			public string name { get { return _name; } set { _name = value; } }

			public string headpic { get { return _headpic; } set { _headpic = value; } }

			public int score { get { return _score; } set { _score = value; } }

			public int roundScore { get { return _roundScore; } set { _roundScore = value; } }

			public bool win { get { return _win; } set { _win = value; } }

			public int index { get { return _index; } }
		}

		List<PlayerSett> _setts;
		bool _isFinal;

		public bool isFinal { get { return _isFinal; } }

		public RoundSett ()
		{
			_setts = new List<PlayerSett> ();
			_isFinal = false;
		}

		public void Init (HashtableEx initArgs)
		{
			_setts.Clear ();

			int winIndex = initArgs.GetInt ("winIndex");
			_isFinal = initArgs.GetBoolean ("isFinalRound");
			HashtableEx players = initArgs.GetMapEx ("players");

			for (int index = 1; index <= 5; ++index) {
				PlayerSett playerSett = new PlayerSett (index);

				GamePlayer gamePlayer = GameDataKernel.inst.GetPlayer (index);
				if (gamePlayer != null) {
					playerSett.name = gamePlayer.name;
					playerSett.headpic = gamePlayer.headpic;

					if (players.ContainsKey (index + "")) {
						playerSett.win = (index == winIndex);

						HashtableEx playerArgs = players.GetMapEx ("" + index);
						playerSett.score = playerArgs.GetInt ("score");
						playerSett.roundScore = playerArgs.GetInt ("roundScore");

						gamePlayer.gold = playerSett.score;
					}
				}

				_setts.Add (playerSett);
			}
		}

		public List<PlayerSett> setts { get { return _setts; } }
	}

	///////////////////////////////////////////////////////////////////////////
	// 游戏数据

	// 房间号
	int _roomId;

	public int roomId { get { return _roomId; } set { _roomId = value; } }

	// 服务器地址
	string _serverAddr;

	public string serverAddr { get { return _serverAddr; } set { _serverAddr = value; } }

	// 总轮数
	int _round;

	public int round { get { return _round; } }

	// 当前轮数
	int _curRound;

	public int curRound { get { return _curRound; } }

	// 癞子数
	int _laiziNum;

	public int laiziNum { get { return _laiziNum; } }

	// 庄
	int _dealer;

	public int dealer { get { return _dealer; } }

	// 是否正在打牌
	bool _playing;

	public bool playing { get { return _playing; } }

	// 玩家数据
	GamePlayer[] _players;

	// 玩家位置
	int _playerIndex;

	public int playerIndex { get { return _playerIndex; } }

	// 就绪玩家数量
	int _readyPlayerCount;

	// 当前出牌玩家
	int _playCardPlayerIndex;

	public int playCardPlayerIndex { get { return _playCardPlayerIndex; } }
	// 是否必须出牌
	bool _mustPlayCard;

	public bool mustPlayCard { get { return _mustPlayCard; } }

	// 获取本地玩家数据
	public GamePlayer mainPlayer {
		get {
			return _players [_playerIndex - 1];
		}
	}

	// 获取玩家
	public GamePlayer GetPlayer (int index)
	{
		return _players [index - 1];
	}

	// 上个玩家出牌
	GameCalcHelper.CalcResult _lastPlayedCards;

	public GameCalcHelper.CalcResult lastPlayedCards { get { return _lastPlayedCards; } }

	// 上局结算数据
	RoundSett _roundSett;

	public RoundSett roundSett { get { return _roundSett; } }

	// 是否第一局
	bool _firstRound;

	// 是否重连
	bool _reconnecting;

	///////////////////////////////////////////////////////////////////////////

	public GameDataKernel ()
	{
		_players = new GamePlayer[5];
		_lastPlayedCards = new GameCalcHelper.CalcResult ();
		_roundSett = new RoundSett ();
		_firstRound = true;
		_reconnecting = false;
	}

	///////////////////////////////////////////////////////////////////////////
	// 接口

	public void HandleMessage (IOMessage message)
	{
		switch (message.code) {
		case IOCmds.SMSG_JOIN_ROOM:
			HandleJoinRoom (message.args);
			break;
		case IOCmds.SMSG_SET_PLAYER_RECONNECT:
			HandlePlayerReconnect (message.args);
			break;
		case IOCmds.SMSG_PLAYER_JOIN_ROOM:
			HandlePlayerJoinRoom (message.args);
			break;
		case IOCmds.SMSG_SET_PLAYER_READY:
			HandlePlayerReady (message.args);
			break;
		case IOCmds.SMSG_PLAYER_CANCEL_READY:
			HandlePlayerNotReady (message.args);
			break;
		case IOCmds.SMSG_SET_ROOM_INFO:
			HandleSetRoomInfo (message.args);
			break;
		case IOCmds.SMSG_SET_PLAYER_CARDS:
			HandleSetPlayerCards (message.args);
			break;
		case IOCmds.SMSG_SET_PLAY_CARD_PLAYER:
			HandleSetPlayCardPlayer (message.args);
			break;
		case IOCmds.SMSG_SET_CARD_NUM:
			HandleSetCardNum (message.args);
			break;
		case IOCmds.SMSG_SET_PLAYED_CARD:
			HandleSetPlayedCard (message.args);
			break;
		case IOCmds.SMSG_PLAYER_PASS:
			HandlePlayerPass (message.args);
			break;
		case IOCmds.SMSG_SEND_NEW_CARD:
			HandleSendNewCard (message.args);
			break;
		case IOCmds.SMSG_SET_ROUND_SETTEMENT:
			HandleRoundSett (message.args);
			break;
		case IOCmds.SMSG_DEALER_CHANGED:
			HandleDealerChange (message.args);
			break;
		case IOCmds.SMSG_BROADCAST_MESSAGE:
			HandleBrocastMessage (message.args);
			break;
		case IOCmds.SMSG_SET_PLAYER_EXIT_ROOM:
			HandlePlayerExitRoom (message.args);
			break;
		case IOCmds.SMSG_SET_PLAYER_OFFLINE:
			HandlePlayerOffline (message.args);
			break;
		case IOCmds.SMSG_SET_PLAYER_ONLINE:
			HandlePlayerOnline (message.args);
			break;
		}
	}

	///////////////////////////////////////////////////////////////////////////
	// 消息处理程序

	void HandleJoinRoom (HashtableEx reqArgs)
	{
		// 重置数据
		_lastPlayedCards.type = 0;
		_firstRound = true;
		_readyPlayerCount = 0;
		_reconnecting = false;

		// 数据初始化
		_round = reqArgs.GetInt ("round");
		_laiziNum = reqArgs.GetInt ("anyCardNum");
		_curRound = 0;
		_dealer = reqArgs.GetInt ("dealer");
		_playing = reqArgs.GetBoolean ("playing");

		// 界面信息初始化
		UIGameWindow.inst.roomInfo.SetRoomId (_roomId);
		UIGameWindow.inst.roomInfo.SetLaiziNum (_laiziNum);
		UIGameWindow.inst.roomInfo.SetRound (_round, _curRound);

		_playerIndex = reqArgs.GetInt ("playerIndex");

		HashtableEx playerDats = reqArgs.GetMapEx ("players");
		foreach (string eKey in playerDats.keys) {
			int playerIndex = Int32.Parse (eKey);
			_players [playerIndex - 1] = new GamePlayer ();
			_players [playerIndex - 1].Init (playerDats.GetMapEx (eKey), playerIndex);
			if (playerIndex == 1) {
				_players [playerIndex - 1].master = true;
			}
			UIGameWindow.inst.GetPlayer (playerIndex).SetInfo (_players [playerIndex - 1]);
		}

		UIGameWindow.inst.SetMasterType (_playerIndex == 1);
		UIGameWindow.inst.gameMaskLayerVisible = _playing;
	}

	// 重连
	void HandlePlayerReconnect (HashtableEx reqArgs)
	{
		// 重置数据
		_lastPlayedCards.type = 0;
		_firstRound = true;
		_readyPlayerCount = 0;
		_reconnecting = true;

		// 数据初始化
		_round = reqArgs.GetInt ("round");
		_laiziNum = reqArgs.GetInt ("anyCardNum");
		_curRound = reqArgs.GetInt ("curRound");
		_dealer = reqArgs.GetInt ("dealer");
		_playing = reqArgs.GetBoolean ("playing");

		int curPlay = reqArgs.GetInt ("curPlay");

		// 界面信息初始化
		UIGameWindow.inst.roomInfo.SetRoomId (_roomId);
		UIGameWindow.inst.roomInfo.SetLaiziNum (_laiziNum);
		UIGameWindow.inst.roomInfo.SetRound (_round, _curRound);

		// 初始化玩家基础数据
		_playerIndex = reqArgs.GetInt ("playerIndex");

		HashtableEx playerDats = reqArgs.GetMapEx ("players");
		foreach (string eKey in playerDats.keys) {
			int playerIndex = Int32.Parse (eKey);
			_players [playerIndex - 1] = new GamePlayer ();
			_players [playerIndex - 1].Init (playerDats.GetMapEx (eKey), playerIndex);
			if (playerIndex == 1) {
				_players [playerIndex - 1].master = true;
			}
			if (playerIndex == _dealer) {
				_players [playerIndex - 1].dealer = true;
			}
			if (_players [playerIndex - 1].ready) {
				_readyPlayerCount += 1;
			}
			UIGameWindow.inst.GetPlayer (playerIndex).SetInfo (_players [playerIndex - 1]);
		}

		UIGameWindow.inst.SetMasterType (_playerIndex == 1);
		UIGameWindow.inst.gameMaskLayerVisible = false;

		if (_playing) {
			// 显示出得牌
			ArrayList playedCardArr = reqArgs.GetList ("subRoundPlayCards");
			foreach (object playedArr in playedCardArr) {
				HashtableEx playedItem = new HashtableEx ((Hashtable)playedArr);
				HandleSetPlayedCard (playedItem, false);
			}

			UIGameWindow.inst.HiddenAllBtns ();
			GameProcedure.inst.SwitchState (Game_GamingState.inst);

			_playCardPlayerIndex = curPlay;
			_mustPlayCard = reqArgs.GetBoolean ("isFirst");

			UIGameWindow.inst.SetPlayCardPlayerIndex (_playCardPlayerIndex);
			if (_playCardPlayerIndex == _playerIndex) {
				UIGameWindow.inst.GetMainPlayer ().buttonsVisible = true;
				UIGameWindow.inst.GetMainPlayer ().HiddPlayedCards ();
			}
		} else if ((_readyPlayerCount >= READY_PLAYERS_COUNT) && (_playerIndex == 1)) {
			UIGameWindow.inst.ShowStartGameBtn (true);
		}
	}

	void HandlePlayerJoinRoom (HashtableEx reqArgs)
	{
		int playerIndex = reqArgs.GetInt ("playerIndex");
		_players [playerIndex - 1] = new GamePlayer ();
		_players [playerIndex - 1].Init (reqArgs.GetMapEx ("player"), playerIndex);
		UIGameWindow.inst.GetPlayer (playerIndex).SetInfo (_players [playerIndex - 1]);
	}

	void HandlePlayerReady (HashtableEx reqArgs)
	{
		int playerIndex = reqArgs.GetInt ("playerIndex");
		_players [playerIndex - 1].ready = true;
		UIGameWindow.inst.GetPlayer (playerIndex).SetInfo (_players [playerIndex - 1]);

		_readyPlayerCount += 1;

		if (1 != _playerIndex) {
			if (playerIndex == _playerIndex) {
				if (_firstRound) {
					UIGameWindow.inst.ShowPrepareBtn (false);
				}
			}
		} else if ((_readyPlayerCount >= READY_PLAYERS_COUNT) 
			&& (_players[_dealer - 1].ready || (_dealer == _playerIndex))) {
			// 房间准备人数大于等于3个并且庄家已经准备
			UIGameWindow.inst.ShowStartGameBtn (true);
		}
	}

	void HandlePlayerNotReady (HashtableEx reqArgs)
	{
		int playerIndex = reqArgs.GetInt ("playerIndex");
		_players [playerIndex - 1].ready = false;
		UIGameWindow.inst.GetPlayer (playerIndex).SetInfo (_players [playerIndex - 1]);

		_readyPlayerCount -= 1;

		if (1 != _playerIndex) {
			if (playerIndex == _playerIndex) {
				if (_firstRound) {
					UIGameWindow.inst.ShowPrepareBtn (true);
				}
			}
		} else if ((_readyPlayerCount < READY_PLAYERS_COUNT) 
			|| (!_players[_dealer - 1].ready)) {
			UIGameWindow.inst.ShowStartGameBtn (false);
		}
	}

	void HandleSetRoomInfo (HashtableEx reqArgs)
	{
		// 数据初始化
		_playing = true;

		_dealer = reqArgs.GetInt ("dealer");
		_curRound = reqArgs.GetInt ("curRound");
		UIGameWindow.inst.roomInfo.SetRound (_round, _curRound);

		foreach (GamePlayer player in _players) {
			if (player != null) {
				player.dealer = false;
			}
		}
		_players [_dealer - 1].dealer = true;

		UpdatePlayersUI ();

		if (_firstRound) {
			ShowIpSame ();
		}
	}

	void HandleSetPlayerCards (HashtableEx reqArgs)
	{
		foreach (GamePlayer player in _players) {
			if (player != null && player.ready) {
				if (player.index == _playerIndex) {
					player.cards.AddRange (reqArgs.GetInts2 ("cards"));
				} else {
					if (player.index == _dealer) {
						player.cards.AddRange (new int[]{ 0, 0, 0, 0, 0, 0 });
					} else {
						player.cards.AddRange (new int[]{ 0, 0, 0, 0, 0 });
					}
				}
			}
		}

		UpdatePlayersUI ();
	}

	void HandleSetPlayCardPlayer (HashtableEx reqArgs)
	{
		int playerIndex = reqArgs.GetInt ("playerIndex");
		_playCardPlayerIndex = playerIndex;
		_mustPlayCard = reqArgs.GetBoolean ("isFirst");

		UIGameWindow.inst.SetPlayCardPlayerIndex (_playCardPlayerIndex);
		if (_playCardPlayerIndex == _playerIndex) {
			UIGameWindow.inst.GetMainPlayer ().buttonsVisible = true;
			UIGameWindow.inst.GetMainPlayer ().HiddPlayedCards ();
		}
	}

	void HandleSetCardNum (HashtableEx reqArgs)
	{
		int cardNum = reqArgs.GetInt ("cardNum");
		UIGameWindow.inst.playCardIndicator.SetRemainCardNum (cardNum);
	}

	void HandleSetPlayedCard (HashtableEx reqArgs, bool decCards = true)
	{
		int playerIndex = reqArgs.GetInt ("playerIndex");
		int[] cards = reqArgs.GetInts2 ("cards");

		_lastPlayedCards.type = reqArgs.GetInt ("type");
		_lastPlayedCards.cards.Clear ();
		_lastPlayedCards.cards.AddRange (cards);
		_lastPlayedCards.maxCard = reqArgs.GetInt ("point");

		// 播放音效
		string audioName;
		if (_players [playerIndex - 1].sex == 1) {
			audioName = "man/";
		} else {
			audioName = "woman/";
		}
		switch (_lastPlayedCards.type) {
		case GameEnum.CardType.TYPE_BOMB:
			audioName += "zhadan";
			break;
		case GameEnum.CardType.TYPE_ORDER:
			audioName += "shunzi";
			break;
		case GameEnum.CardType.TYPE_ORDER_PAIR:
			audioName += "liandui";
			break;
		case GameEnum.CardType.TYPE_PAIR:
			int plydCad = _lastPlayedCards.maxCard;
			if (plydCad == 14)
				plydCad = 1;
			audioName += "dui" + plydCad;
			break;
		case GameEnum.CardType.TYPE_SINGLE:
			plydCad = _lastPlayedCards.maxCard;
			if (plydCad == 14)
				plydCad = 1;
			audioName += plydCad;
			break;
		}
		AudioController.inst.PlayEffect (audioName);

		// 更新手牌
		if (decCards) {
			_players [playerIndex - 1].DecCards (cards, playerIndex == _playerIndex);
		}
		UIGameWindow.inst.GetPlayer (playerIndex).SetInfo (_players [playerIndex - 1]);
		UIGameWindow.inst.GetPlayer (playerIndex).ShowPlayedCards (cards, _lastPlayedCards.type, _lastPlayedCards.maxCard);

		if (playerIndex == _playerIndex) {
			UIGameWindow.inst.GetMainPlayer ().DeSelectCards ();
		}

		if (decCards) {
			// 播放特效
			GameController.inst.proceduar.paused = true;
			EffectController.inst.PlayCardTypeEffect (_lastPlayedCards.type, () => {
				GameController.inst.proceduar.paused = false;
			});
		}
	}

	void HandlePlayerPass (HashtableEx reqArgs)
	{
		int playerIndex = reqArgs.GetInt ("playerIndex");

		string audioName;
		if (_players [playerIndex - 1].sex == 1) {
			audioName = "man/buyao";
		} else {
			audioName = "woman/buyao";
		}
		int random = new Random ().Next (1, 4);
		audioName += random;
		AudioController.inst.PlayEffect (audioName);

		if (_playCardPlayerIndex == _playerIndex) {
			UIGameWindow.inst.GetMainPlayer ().buttonsVisible = false;
		}
	}

	void HandleSendNewCard (HashtableEx reqArgs)
	{
		_lastPlayedCards.type = 0;
		_lastPlayedCards.cards.Clear ();
		_lastPlayedCards.maxCard = 0;

		foreach (GamePlayer player in _players) {
			if (player != null) {
				UIGameWindow.inst.GetPlayer (player.index).HiddPlayedCards ();
			}
		}

		int playerIndex = reqArgs.GetInt ("playerIndex");
		_players [playerIndex - 1].AddCard (reqArgs.GetInt ("card"), playerIndex == _playerIndex);
		UIGameWindow.inst.GetPlayer (playerIndex).SetInfo (_players [playerIndex - 1]);
	}

	void HandleRoundSett (HashtableEx reqArgs)
	{
		UIGameWindow.inst.gameMaskLayerVisible = false;
		_roundSett.Init (reqArgs);

		// 暂停消息处理
		GameController.inst.proceduar.paused = true;
		// 短暂延迟后弹出结算
		TimerController.inst.CallAfter (2.0f, args => {
			GameProcedure.inst.SwitchState (Game_SettState.inst);
		});
	}

	void HandleDealerChange (HashtableEx reqArgs)
	{
		int dealerIndex = reqArgs.GetInt ("dealer");

		_players [_dealer - 1].dealer = false;
		UIGameWindow.inst.GetPlayer (_dealer).SetInfo (_players [_dealer - 1]);

		_dealer = dealerIndex;
		_players [_dealer - 1].dealer = true;
		UIGameWindow.inst.GetPlayer (_dealer).SetInfo (_players [_dealer - 1]);
	}

	void HandleBrocastMessage (HashtableEx reqArgs)
	{
		int playerIndex = reqArgs.GetInt ("playerIndex");
		string message = reqArgs.GetString ("message");

		if (message.IndexOf ("text://") != -1) {
			// 文本消息
			message = message.Substring (7);
			UIGameWindow.inst.GetPlayer (playerIndex).ShowChatMessage (message);
		} else if (message.IndexOf ("voice://") != -1) {
			// 语音消息
			VoiceChat.inst.OnVoiceMessage (playerIndex, message.Substring (8));
		} else if (message.IndexOf ("fix://") != -1) {
			int id = Convert.ToInt32( message.Substring (6) );
			string audioName;
			if (_players [playerIndex - 1].sex == 1) {
				audioName = "man/fix" + id;
			} else {
				audioName = "woman/fix" + id;
			}
			AudioController.inst.PlayEffect (audioName);
			string txtMsg = "";
			switch (id) {
			case 1: txtMsg = "你太牛啦！"; break;
			case 2: txtMsg = "哈哈！手气真好！"; break;
			case 3: txtMsg = "快点出牌噢！"; break;
			case 4: txtMsg = "今天真高兴！"; break;
			case 5: txtMsg = "你家里是开银行的吧！"; break;
			case 6: txtMsg = "不好意思！我有事儿要先走一步啦！"; break;
			case 7: txtMsg = "你牌打的太好啦！"; break;
			case 8: txtMsg = "大家好！很高兴见到各位！"; break;
			case 9: txtMsg = "怎么又断线了，网络怎么这么差啊！"; break;
			}
			UIGameWindow.inst.GetPlayer (playerIndex).ShowChatMessage (txtMsg);
		}else {
			DebugInfo.Error ("HandleBrocastMessage 未知消息");
		}
	}

	void HandlePlayerExitRoom (HashtableEx reqArgs)
	{
		int playerIndex = reqArgs.GetInt ("playerIndex");
		if (_players [playerIndex - 1].ready) {
			_readyPlayerCount -= 1;

			if ((_playerIndex == 1) && (_readyPlayerCount < READY_PLAYERS_COUNT)) {
				UIGameWindow.inst.ShowStartGameBtn (false);
			}
		}

		_players [playerIndex - 1] = null;
		UIGameWindow.inst.GetPlayer (playerIndex).SetInfo (null);
	}

	void HandlePlayerOffline(HashtableEx reqArgs) {
		int playerIndex = reqArgs.GetInt ("playerIndex");
		_players [playerIndex - 1].online = false;
		UIGameWindow.inst.GetPlayer (playerIndex).SetInfo (_players [playerIndex - 1]);
	}

	void HandlePlayerOnline(HashtableEx reqArgs) {
		int playerIndex = reqArgs.GetInt ("playerIndex");
		_players [playerIndex - 1].online = true;
		UIGameWindow.inst.GetPlayer (playerIndex).SetInfo (_players [playerIndex - 1]);
	}

	///////////////////////////////////////////////////////////////////////////
	// 帮助函数

	/// <summary>
	/// 更新玩家数据
	/// </summary>
	void UpdatePlayersUI ()
	{
		foreach (GamePlayer player in _players) {
			if (player != null) {
				UIGameWindow.inst.GetPlayer (player.index).SetInfo (player);
			}
		}
	}

	public void ResetForContinue ()
	{
		_readyPlayerCount = 0;
		_playing = false;
		_firstRound = false;
		_lastPlayedCards.type = 0;
		_curRound += 1;
		UIGameWindow.inst.roomInfo.SetRound (_round, _curRound);
		UIGameWindow.inst.playCardIndicator.visible = false;

		foreach (GamePlayer player in _players) {
			if (player != null) {
				player.Reset ();

				UIGamePlayer gamePlayer = UIGameWindow.inst.GetPlayer (player.index);
				gamePlayer.SetInfo (player);
				gamePlayer.HiddPlayedCards ();
			}
		}

		//if (_playerIndex == 1) {
		//	UIGameWindow.inst.ShowStartGameBtn (true);
		//}
	}

	// 相同IP提示
	void ShowIpSame ()
	{
		List<List<int>> sames = new List<List<int>> ();
		Hashtable used = new Hashtable ();

		for (int index = 1; index <= 5; ++index) {
			if (used.ContainsKey (index)) {
				continue;
			}
			GamePlayer gamePlayer = GetPlayer (index);
			if (gamePlayer != null) {

				List<int> same = new List<int> ();
				same.Add (index);

				string ip = gamePlayer.ip;

				for (int index2 = 1; index2 <= 5; ++index2) {
					if (used.ContainsKey (index) || (index == index2)) {
						continue;
					}
					GamePlayer gamePlayer2 = GetPlayer (index2);
					if (gamePlayer2 != null) {

						string ip2 = gamePlayer2.ip;

						if (ip == ip2) {
							same.Add (index2);
						}
					}
				}

				if (same.Count > 1) {
					sames.Add (same);
					foreach (int idx in same) {
						used [idx] = 1;
					}
				}
			}
		}

		if (sames.Count == 0) {
			return;
		}

		string msg = "";
		foreach (List<int> same in sames) {
			string msg2 = "";
			for (int i = 0; i < same.Count; ++i) {
				GamePlayer player = _players [same [i] - 1];
				if ((i + 1) != same.Count) {
					msg2 += player.name + ",";
				} else {
					msg2 += player.name + " 来自同一IP。";
				}
			}
			if (msg2 != "") {
				msg += msg2 + "\n";
			}
		}

		UIGameIpTip.Popup (msg);
	}
}
